﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/MasterPage_Edit.master" AutoEventWireup="true" CodeFile="SaleEdit.aspx.cs" Inherits="Sale_SaleEdit" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Title" runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Content" runat="Server">
    <div class="ht-panel-edit">
        <table class="ht-edit-table">
            <tr>
                <th>销售单号：</th>
                <td>
                    <asp:TextBox ID="txt_SaleCode" runat="server" CssClass="ht-textbox"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <th>单据日期：</th>
                <td>
                    <asp:TextBox ID="txt_SaleTime" runat="server" CssClass="ht-textbox Wdate" onfocus="WdatePicker()"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <th>客户名称：</th>
                <td>
                    <asp:DropDownList ID="ddl_CustomerId" runat="server" CssClass="ht-select"></asp:DropDownList>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="ddl_CustomerId" ValidationGroup="Check" ErrorMessage="请选择"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <th>销售员：</th>
                <td>
                    <asp:TextBox ID="txt_Salesman" runat="server" CssClass="ht-textbox"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <th>收款汇率：</th>
                <td>
                    <asp:TextBox ID="txt_ExchangeRate" runat="server" CssClass="ht-textbox"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <th>实付运费：</th>
                <td>
                    <asp:TextBox ID="txt_Freight" runat="server" CssClass="ht-textbox"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <th>收客户运费：</th>
                <td>
                    <asp:TextBox ID="txt_PayFreight" runat="server" CssClass="ht-textbox"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <th>款项：</th>
                <td>
                    <asp:TextBox ID="txt_Payment" runat="server" CssClass="ht-textbox"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <th>状态：</th>
                <td>
                    <asp:RadioButtonList ID="rbl_SaleState" runat="server" RepeatLayout="Flow" CssClass="ht-radiolist" RepeatDirection="Horizontal"></asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <th>备注：</th>
                <td>
                    <asp:TextBox ID="txt_Description" runat="server" TextMode="MultiLine" CssClass="ht-area"></asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
    <p style="margin-top: 5px">
        <a id="btn_AddListItem" href="javascript:void()" onclick="addListItem()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a>
    </p>
    <div class="ht-panel-grid">
        <asp:HiddenField ID="hd_GridValue" runat="server" />
        <table id="grid" class="ht-grid" border="0" style="margin-top: 10px;">
            <thead>
                <tr>
                    <th rowspan="1" colspan="7">进货</th>
                    <th rowspan="1" colspan="4">销售</th>
                    <th rowspan="2" style="width: 80px">销售利润(RMB)</th>
                    <th rowspan="2" style="width: 80px">操作</th>
                </tr>
                <tr>
                    <th rowspan="1" style="width: 140px;">型号</th>
                    <th rowspan="1" style="width: 120px;">供应商</th>
                    <th rowspan="1" style="width: 120px;">日期</th>
                    <th rowspan="1" style="width: 120px;">收据单号</th>
                    <th rowspan="1" style="width: 80px;">数量(PCS)</th>
                    <th rowspan="1" style="width: 80px;">单价(RMB)</th>
                    <th rowspan="1" style="width: 80px;">金额(RMB)</th>
                    <th rowspan="1" style="width: 80px;">单价(RMB)</th>
                    <th rowspan="1" style="width: 80px;">单价(USD)</th>
                    <th rowspan="1" style="width: 80px;">金额(RMB)</th>
                    <th rowspan="1" style="width: 80px;">金额(USD)</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
            <tfoot>
                <tr>
                    <td>合计：</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td date-filedname="PurchaseAmount" style="text-align: right;"><span>0</span></td>
                    <td></td>
                    <td></td>
                    <td date-filedname="SaleAmountRMB" style="text-align: right;"><span>0</span></td>
                    <td date-filedname="SaleAmountUSD" style="text-align: right;"><span>0</span></td>
                    <td date-filedname="SaleProfitRMB" style="text-align: right;"><span>0</span></td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
    </div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Buttoms" runat="Server">
    <asp:LinkButton ID="btn_Modify" runat="server" Text="编辑" CssClass="easyui-linkbutton" data-options="iconCls:'icon-edit'" OnClick="btn_Modify_Click" />
    <asp:LinkButton ID="btn_Save" runat="server" ValidationGroup="Check" Text="保存" CssClass="easyui-linkbutton" data-options="iconCls:'icon-save'"
        OnClick="btn_Save_Click" OnClientClick="return beforeSave()" />
    <a id="btn_Back" runat="server" href="javascript:Cancel()" class="easyui-linkbutton" data-options="iconCls:'icon-back'">返回</a>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Footer" runat="Server">
    <script>
        var $hd_GridValue = $('#<%=hd_GridValue.ClientID%>');

        function addListItem() {
            var _html = '<tr data-SaleDetailId="">'
                    + '<td class="edit-td" date-filedname="PurchaseCode">'
                    + '<input type="text" />'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="SupplierName">'
                    + '<input type="text" />'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="PurchaseTime">'
                    + '<input type="text"class="Wdate" onfocus="WdatePicker()"/>'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="PurchaseReceipt">'
                    + '<input type="text" />'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="Quantity">'
                    + '<input type="text" onchange="onQuantityChange(this)"/>'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="PurchasePrice">'
                    + '<input type="text" onchange="onPurchasePriceChange(this)" />'
                    + '</td>'
                    + '<td date-filedname="PurchaseAmount" style="text-align:right;">'
                    + '<span>0</span>'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="SalePriceRMB">'
                    + '<input type="text" onchange="onSalePriceRMBChange(this)"/>'
                    + '</td>'
                    + '<td class="edit-td" date-filedname="SalePriceUSD">'
                    + '<input type="text" onchange="onSalePriceUSDChange(this)"/>'
                    + '</td>'
                    + '<td date-filedname="SaleAmountRMB" style="text-align:right;">'
                    + '<span>0</span>'
                    + '</td>'
                    + '<td date-filedname="SaleAmountUSD" style="text-align:right;">'
                    + '<span>0</span>'
                    + '</td>'
                    + '<td date-filedname="SaleProfitRMB" style="text-align:right;">'
                    + '<span>0</span>'
                    + '</td>'
                    + '<td>'
                    + '<a href="javascript:void()" onclick="delGridRow(this)">删除</a>'
                    + '</td>'
                    + '</tr>';
            $('#grid').find('tbody').append(_html);
        }

        function delGridRow(obj) {
            $(obj).parent().parent().remove();
        }

        function beforeSave() {
            var $grid = $('#grid');

            var list = Array();

            $grid.find("tbody tr").each(function () {
                var item = new Object();

                item.PurchaseTime = $(this).find("td[date-filedname=PurchaseTime] input").val();
                item.PurchaseCode = $(this).find("td[date-filedname=PurchaseCode] input").val();
                item.PurchaseReceipt = $(this).find("td[date-filedname=PurchaseReceipt] input").val();
                item.Quantity = Number($(this).find("td[date-filedname=Quantity] input").val());
                item.PurchasePrice = Number($(this).find("td[date-filedname=PurchasePrice] input").val());
                item.PurchaseReceipt = $(this).find("td[date-filedname=PurchaseReceipt] input").val();
                item.SalePriceUSD = Number($(this).find("td[date-filedname=SalePriceUSD] input").val());
                item.SalePriceRMB = Number($(this).find("td[date-filedname=SalePriceRMB] input").val());
                item.SupplierName = $(this).find("td[date-filedname=SupplierName] input").val();

                list.push(item);
            });

            var sJson = JSON.stringify(list);
            $hd_GridValue.val(sJson);

            return true;
        }

        function onQuantityChange(o) {
            $(o).val(checkFloat($(o).val()));

            onPurchasePriceChange($(o).parent().parent().find("td[date-filedname=PurchasePrice] input"));
            onSalePriceRMBChange($(o).parent().parent().find("td[date-filedname=SalePriceRMB] input"));
            onSalePriceUSDChange($(o).parent().parent().find("td[date-filedname=SalePriceUSD] input"));
        }

        function onPurchasePriceChange(o) {
            $(o).val(checkFloat($(o).val()));

            var _fPrice = parseFloat($(o).val());
            var _fQty = $(o).parent().parent().find("td[date-filedname=Quantity] input").val();
            var _fAmount = _fQty * _fPrice;
            $(o).parent().parent().find("td[date-filedname=PurchaseAmount] span").text(checkFloat(_fAmount));

            calSaleProfitRMB(o);

            //合计
            var _fSumAmount = 0;
            $('#grid tbody td[date-filedname=PurchaseAmount] span').each(function(){
                _fSumAmount+=Number($(this).text());
            });
            $('#grid tfoot td[date-filedname=PurchaseAmount] span').text(checkFloat(_fSumAmount));
        }

        function onSalePriceRMBChange(o) {
            $(o).val(checkFloat($(o).val()));

            var _fPrice = Number($(o).val());
            var _fQty = $(o).parent().parent().find("td[date-filedname=Quantity] input").val();
            var _fAmount = _fQty * _fPrice;
            $(o).parent().parent().find("td[date-filedname=SaleAmountRMB] span").text(checkFloat(_fAmount));

            calSaleProfitRMB(o);

            //合计
            var _fSumAmount = 0;
            $('#grid tbody td[date-filedname=SaleAmountRMB] span').each(function(){
                _fSumAmount+=Number($(this).text());
            });
            $('#grid tfoot td[date-filedname=SaleAmountRMB] span').text(checkFloat(_fSumAmount));
        }

        function onSalePriceUSDChange(o) {
            $(o).val(checkFloat($(o).val()));

            var _fPrice = Number($(o).val());
            var _fQty = $(o).parent().parent().find("td[date-filedname=Quantity] input").val();
            var _fAmount = _fQty * _fPrice;
            $(o).parent().parent().find("td[date-filedname=SaleAmountUSD] span").text(checkFloat(_fAmount));

            $('#grid').find("td[date-filedname=SaleAmountUSD] span").text(checkFloat(_fAmount));

            //合计
            var _fSumAmount = 0;
            $('#grid tbody td[date-filedname=SaleAmountUSD] span').each(function(){
                _fSumAmount+=Number($(this).text());
            });
            $('#grid tfoot td[date-filedname=SaleAmountUSD] span').text(checkFloat(_fSumAmount));
        }

        function calSaleProfitRMB(o){
            var _fPurchaseAmount = checkFloat($(o).parent().parent().find("td[date-filedname=PurchaseAmount] span").text());
            var _fSaleAmountRMB = checkFloat($(o).parent().parent().find("td[date-filedname=SaleAmountRMB] span").text());
            var _fSaleProfitRMB = _fSaleAmountRMB - _fPurchaseAmount;

            $(o).parent().parent().find("td[date-filedname=SaleProfitRMB] span").text(checkFloat(_fSaleProfitRMB));

            //合计
            var _fSumAmount = 0;
            $('#grid tbody td[date-filedname=SaleProfitRMB] span').each(function(){
                _fSumAmount+=Number($(this).text());
            });
            $('#grid tfoot td[date-filedname=SaleProfitRMB] span').text(checkFloat(_fSumAmount));
        }

    </script>

    <!--数据列表加载-->
    <script type="text/javascript">
        var sQueryString = "";
        var sSaleId = '<%=SaleId%>';
        var iIsEdit = <%=(Action=="HAdd"||Action=="Modify")?1:0%>;

        $(function () {
            if(iIsEdit==1){
                $('#btn_AddListItem').show();
            }else{
                $('#btn_AddListItem').hide();
            }

            //加载数据
            refreshGrid();
        });

        function doSearch(value) {
            sQueryString = value;
            refreshGrid();
        }

        function refreshGrid() {
            if (sSaleId == "") {
                return;
            }
            $('#grid').BangleGrid({
                pager: '#pager',
                url: 'SaleEdit.aspx/GetGridDataHtml',
                query: { QueryString: sQueryString, SaleId: sSaleId ,IsEdit:iIsEdit},
                afterSuccess: function () {
                    $('#grid tbody tr td[date-filedname=Quantity] input').each(function () {
                        onQuantityChange(this);
                    });
                }
            });
        }


        //价格输入
        function checkFloat(str) {
            str+='';
            var _num = parseFloat(str).toFixed(2);
            _num = (_num == 'NaN') ? '' : _num;
            return _num;
        }
    </script>
</asp:Content>

